from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
from google.appengine.ext.webapp import template
import os
import logging
from models import Project, Category, Ticket
from forms import ProjectForm

class ItemHandler(webapp.RequestHandler):

    def get(self, key):
        category = Category.get(key)
        phases = {}
        for t in Ticket.gql('WHERE category = :1 order by milestone, status desc', category.name):
            if t.phase not in phases.keys():
                phases[t.phase] = []
            phases[t.phase].append(t)
        context = {
            'title': category.name,
            'section': 'Projects',
            'category': category,
            'phases': phases,
        }
        path = os.path.join(os.path.dirname(__file__), 
            '../templates/category.html')        
        self.response.out.write(template.render(path, context))

    def post(self, key):
        category = Category.get(key)
        category.calculate_hours()

def main():
    application = webapp.WSGIApplication([
        (r'/categories/(.+)/', ItemHandler),
        ], debug=True)
    util.run_wsgi_app(application)

if __name__ == '__main__':
    main()
